package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysCusService1Bi;
import com.ruoyi.system.service.ISysCusService1BiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysXsshWorkOrderBi;
import com.ruoyi.system.service.ISysXsshWorkOrderBiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * SysXsshWorkOrderBiController
 * 
 * @author SYF
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/system/SysXsshWorkOrderBi")
public class SysXsshWorkOrderBiController extends BaseController {

    @Autowired
    private ISysXsshWorkOrderBiService sysXsshWorkOrderBiService;

    @Autowired
    private ISysCusService1BiService cusService1BiService;

    /**
     * 查询SysXsshWorkOrderBi列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysXsshWorkOrderBi sysXsshWorkOrderBi) {
        startPage();
        List<SysXsshWorkOrderBi> list = sysXsshWorkOrderBiService.selectSysXsshWorkOrderBiList(sysXsshWorkOrderBi);
        return getDataTable(list);
    }

    /**
     * 导出SysXsshWorkOrderBi列表
     */
    @Log(title = "SysXsshWorkOrderBi", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysXsshWorkOrderBi sysXsshWorkOrderBi) {
        List<SysXsshWorkOrderBi> list = sysXsshWorkOrderBiService.selectSysXsshWorkOrderBiList(sysXsshWorkOrderBi);
        ExcelUtil<SysXsshWorkOrderBi> util = new ExcelUtil<SysXsshWorkOrderBi>(SysXsshWorkOrderBi.class);
        util.exportExcel(response, list, "SysXsshWorkOrderBi数据");
    }

    /**
     * 获取SysXsshWorkOrderBi详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sysXsshWorkOrderBiService.selectSysXsshWorkOrderBiById(id));
    }

    /**
     * 新增SysXsshWorkOrderBi
     */
    @Log(title = "SysXsshWorkOrderBi", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysXsshWorkOrderBi order) {
        order.setWkDay(order.getWkYear());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        order.setId(uuid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(order.getWkYear());
        }catch (ParseException e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取一年中的第几周，具体取决于 Calendar 类的设置
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(order.getYear()));
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = sdf.format(cal.getTime());
        // 获取周一和周日的日期
        Calendar endOfWeek = Calendar.getInstance();
        endOfWeek.setTime(cal.getTime());
        endOfWeek.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = sdf.format(endOfWeek.getTime());
        String wkYear = monday+"至"+sunday+"("+weekOfYear+"wk)";
        order.setWkYear(wkYear);
        sysXsshWorkOrderBiService.insertSysXsshWorkOrderBi(order);
        SysCusService1Bi cus = new SysCusService1Bi();
        cus.setOrderId(order.getId());
        cus.setQd(sdf.format(new Date()));
        cus.setShType("销售售后");
        cus.setZmpz(order.getBrand());
        cus.setArea(order.getArea());
        cus.setShengzZhouqu("销售售后");
        cus.setGid(order.getQuestion());
        cus.setQty(1L);
        cus.setWkYear(wkYear);
        cus.setCus(order.getCus());
        cus.setSaleman(order.getSlaeman());
        cusService1BiService.insertSysCusService1Bi(cus);
        return AjaxResult.success();
    }

    /**
     * 修改SysXsshWorkOrderBi
     */
    @Log(title = "SysXsshWorkOrderBi", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysXsshWorkOrderBi order) {
        order.setWkDay(order.getWkYear());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(order.getWkYear());
        }catch (ParseException e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取一年中的第几周，具体取决于 Calendar 类的设置
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(order.getYear()));
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = sdf.format(cal.getTime());
        // 获取周一和周日的日期
        Calendar endOfWeek = Calendar.getInstance();
        endOfWeek.setTime(cal.getTime());
        endOfWeek.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = sdf.format(endOfWeek.getTime());
        String wkYear = monday+"至"+sunday+"("+weekOfYear+"wk)";
        order.setWkYear(wkYear);
        sysXsshWorkOrderBiService.updateSysXsshWorkOrderBi(order);
        cusService1BiService.deleteCusService1ByOrderId(order.getId());
        SysCusService1Bi cus = new SysCusService1Bi();
        cus.setOrderId(order.getId());
        cus.setQd(sdf.format(new Date()));
        cus.setShType("销售售后");
        cus.setZmpz(order.getBrand());
        cus.setArea(order.getArea());
        cus.setShengzZhouqu("销售售后");
        cus.setGid(order.getQuestion());
        cus.setQty(1L);
        cus.setWkYear(wkYear);
        cus.setCus(order.getCus());
        cus.setSaleman(order.getSlaeman());
        cusService1BiService.insertSysCusService1Bi(cus);
        return AjaxResult.success();
    }

    /**
     * 删除SysXsshWorkOrderBi
     */
    @Log(title = "SysXsshWorkOrderBi", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wkYears}")
    public AjaxResult remove(@PathVariable String[] wkYears)
    {
        return toAjax(sysXsshWorkOrderBiService.deleteSysXsshWorkOrderBiByWkYears(wkYears));
    }
}
